package cn.db101.fairy.controller

import cn.db101.fairy.entity.FairyDetail
import cn.db101.fairy.entity.User
import cn.db101.fairy.entity.UserCollect
import cn.db101.fairy.model.FairyDetailParam
import cn.db101.fairy.model.ResultModel
import cn.db101.fairy.model.WxLoginModel
import cn.db101.fairy.service.FairyCarouselService
import cn.db101.fairy.service.FairyService
import cn.db101.fairy.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

/**
 *
 * @author：zooooooooy
 * @date: 2022/9/14 - 11:35
 */
@RestController
@RequestMapping("v1/fairy")
open class FairyController {

    @Autowired
    open lateinit var fairyService: FairyService

    @Autowired
    open lateinit var userService: UserService

    @Autowired
    open lateinit var fairyCarouselService: FairyCarouselService

    /**
     * 查询所有分类
     */
    @GetMapping("all")
    fun all(): ResultModel<Any> {

        return ResultModel.wrapSuccessResult(fairyService.all())
    }

    /**
     * 查询分类下的所有文章
     */
    @PostMapping("detailList")
    fun detailList(@RequestBody fairyDetailParam: FairyDetailParam): ResultModel<Any> {

        return ResultModel.wrapSuccessResult(fairyService.detailList(fairyDetailParam))
    }

    @PostMapping("page")
    fun page(@RequestBody fairyDetailParam: FairyDetailParam): ResultModel<Any> {

        return ResultModel.wrapSuccessResult(fairyService.page(fairyDetailParam))
    }

    @PostMapping("detail")
    fun detail(@RequestBody fairyDetail: FairyDetail): ResultModel<Any?> {

        return ResultModel.wrapSuccessResult(fairyService.detail(fairyDetail.id))
    }

    @PostMapping("wxLogin")
    fun wxLogin(@RequestBody wxLoginModel: WxLoginModel): ResultModel<Any?> {

        return ResultModel.wrapSuccessResult(userService.wxLogin(wxLoginModel.code!!))
    }

    @PostMapping("updateUser")
    fun updateUser(@RequestBody user: User): ResultModel<Any> {

        return ResultModel.wrapSuccessResult(userService.updateUser(user))
    }

    /**
     * 轮播列表
     */
    @PostMapping("carouselList")
    fun carouselList(): ResultModel<Any> {

        return ResultModel.wrapSuccessResult(fairyCarouselService.carouselList())
    }

    /**
     * 查询浏览量最多的6篇文章
     */
    @PostMapping("findTop6Views")
    fun findTop6Views(): ResultModel<Any> {

        return ResultModel.wrapSuccessResult(fairyService.findTop6Views())
    }

    /**
     * 查询收藏数量最多的6篇文章
     */
    @PostMapping("findTop6Collects")
    fun findTop6Collects(): ResultModel<Any> {

        return ResultModel.wrapSuccessResult(fairyService.findTop6Collects())
    }

    /**
     * 增加文章浏览量
     */
    @PostMapping("addViews")
    fun addViews(@RequestBody fairyDetail: FairyDetail): ResultModel<Any> {

        fairyService.addViews(fairyDetail)
        return ResultModel.wrapSuccessResult()
    }

    /**
     * 收藏故事
     */
    @PostMapping("toggleCollect")
    fun addCollect(@RequestBody userCollect: UserCollect): ResultModel<Any> {

        userService.toggleCollect(userCollect)
        return ResultModel.wrapSuccessResult()
    }

    /**
     * 查询是否收藏
     */
    @PostMapping("queryCollect")
    fun queryCollect(@RequestBody userCollect: UserCollect): ResultModel<Any?> {

        return ResultModel.wrapSuccessResult(userService.queryCollect(userCollect))
    }

    /**
     * 查询用户收藏列表
     */
    @PostMapping("collectList")
    fun collectList(@RequestBody userCollect: UserCollect): ResultModel<Any> {

        return ResultModel.wrapSuccessResult(fairyService.collectList(userCollect))
    }

    @PostMapping("search")
    fun search(@RequestBody fairyDetail: FairyDetail): ResultModel<Any> {

        return ResultModel.wrapSuccessResult(fairyService.search(fairyDetail))
    }

}
